--见机决程
local jianjijuecheng = fk.CreateSkill {
    name = "mcdh_jianjijuecheng",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

jianjijuecheng:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self)  and  player:usedSkillTimes(self.name,Player.HistoryTurn) == 0 then
                for _, move in ipairs(data) do
                    if move.from == player.id then
                        for _, info in ipairs(move.moveInfo) do
                            if info.fromArea == Card.PlayerHand then
                                return player:isKongcheng()
                            end
                        end
                    end
                end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets =  table.filter(room:getOtherPlayers(player),function(p) return lib.mcdh_getLoseammo(player) > 0  end)
        local to = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#jianjijuecheng-reload",
            skill_name = self.name,
            cancelable = true
          })
        if #to ~= 0 then
            self.cost_data = to[1]
            return true
        end

    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = self.cost_data
        lib.LoadAmmo(player,to,2,self.name)
    end
})

jianjijuecheng:addEffect(lib.AfterAmmoAndCharge,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self)  and  player:usedSkillTimes(self.name,Player.HistoryTurn) == 0 then
            return player:getMark("mcdh_ammo") ==  0 and target == player and data.type == "ammo"
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = room:getOtherPlayers(player)
        local to = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#jianjijuecheng-reload",
            skill_name = self.name,
            cancelable = true
          })
        if #to ~= 0 then
            self.cost_data = to[1]
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = self.cost_data
        to:drawCards(2,self.name)
    end
})


Fk:loadTranslationTable {
    ["mcdh_jianjijuecheng"] = "见机决程",
    [":mcdh_jianjijuecheng"] = "每回合限一次：1.当你失去最后的手牌后，你可以令一名其他角色装填两枚弹药；2.当你失去最后的弹药后，你可以令一名其他角色摸两张牌。",
    ["#jianjijuecheng-reload"] = "见机决程:令一名其他角色装填两枚弹药",
    ["#jianjijuecheng-draw"] = "见机决程:令一名其他角色摸两张牌",
}

return jianjijuecheng